package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.it2297.component.AbstractPagination;
import com.it2297.main.App;
import com.it2297.model.UsedItem.SortBy;
import com.it2297.model.UsedItem.Status;
import com.it2297.model.UsedItem.Type;
import com.it2297.renderer.UsedItemCellRenderer;

@SuppressWarnings("serial")
public class UsedItemBrowseView extends JPanel
{
	private final JTable table = new JTable();
	private final UsedItemCellRenderer tableCellRenderer = new UsedItemCellRenderer();
	private final JPanel toolBarPanel = new JPanel();
	private final JToolBar toolBar = new JToolBar();
	private final JButton btnNew = new JButton("");
	private final JButton btnEdit = new JButton("");
	private final JButton btnRemove = new JButton("");
	private final JComboBox comboBox = new JComboBox(SortBy.values());
	private final JLabel lblSortBy = new JLabel("Sort:");
	private final JScrollPane scrollPane = new JScrollPane();
	private AbstractPagination pagination;
	private final JLabel lblFilter = new JLabel("Filter:");
	private final JComboBox comboBox_1 = new JComboBox();
	private final JToolBar toolBar_1 = new JToolBar();
	private final Component horizontalStrut = Box.createHorizontalStrut(20);
	private final JLabel lblStatus = new JLabel("Status:");
	private final JComboBox comboBox_2 = new JComboBox();
	private final DefaultTableModel tableModel = new DefaultTableModel(0, 2)
	{
		@Override
		public boolean isCellEditable(int row, int column)
		{
			return false;
		}
	};
	private final JButton btnSearch = new JButton("");
	private final Component horizontalStrut_2 = Box.createHorizontalStrut(20);
	private final JButton btnReset = new JButton("");

	public UsedItemBrowseView()
	{
		initGUI();
	}

	private void initGUI()
	{
		this.setBounds(100, 100, 750, 570);
		this.setLayout(new BorderLayout(0, 0));
		this.add(scrollPane, BorderLayout.CENTER);

		scrollPane.setViewportView(table);

		table.setRowHeight(200);
		table.setShowGrid(false);
		table.setFocusable(true);
		table.setModel(tableModel);
		table.setBackground(new Color(240, 240, 240));
		table.setTableHeader(null);
		table.setCellSelectionEnabled(true);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(tableCellRenderer);
		toolBarPanel.setLayout(new BoxLayout(toolBarPanel, BoxLayout.LINE_AXIS));
		toolBar.setBorderPainted(false);
		toolBar.setAlignmentX(Component.LEFT_ALIGNMENT);

		toolBar.setFloatable(false);

		add(toolBarPanel, BorderLayout.NORTH);
		btnNew.setIcon(new ImageIcon(UsedItemBrowseView.class.getResource("/com/alee/extended/filechooser/icons/folder_new.png")));
		btnNew.setFocusable(false);

		toolBar.add(btnNew);
		btnEdit.setIcon(new ImageIcon(UsedItemBrowseView.class.getResource("/com/alee/extended/filechooser/icons/edit.png")));
		btnEdit.setFocusable(false);

		toolBar.add(btnEdit);
		btnRemove.setIcon(new ImageIcon(UsedItemBrowseView.class.getResource("/com/alee/extended/filechooser/icons/cancel.png")));
		btnRemove.setFocusable(false);

		toolBar.add(btnRemove);
		toolBar.add(horizontalStrut);
		toolBar.add(lblStatus);
		toolBar.add(comboBox_2);
		toolBar_1.setAlignmentX(Component.LEFT_ALIGNMENT);
		toolBar_1.setFloatable(false);

		toolBarPanel.add(toolBar_1);
		btnReset.setIcon(new ImageIcon(UsedItemBrowseView.class.getResource("/com/alee/extended/filechooser/icons/refresh.png")));

		toolBar_1.add(btnReset);
		toolBar_1.add(btnSearch);
		btnSearch.setIcon(new ImageIcon(UsedItemBrowseView.class.getResource("/com/alee/extended/ninepatch/icons/min.png")));
		lblSortBy.setBorder(new EmptyBorder(0, 5, 0, 0));
		lblSortBy.setIcon(null);
		toolBar_1.add(lblSortBy);
		toolBar_1.add(comboBox);
		comboBox.setFocusable(false);

		toolBar_1.add(horizontalStrut_2);
		lblFilter.setIcon(null);
		toolBar_1.add(lblFilter);
		toolBar_1.add(comboBox_1);
	}

	public void addStatusItem(Object[] objects)
	{
		for (Object object : objects)
		{
			comboBox_2.addItem(object);
		}
	}

	public void addFilterItems(Object[] objects)
	{
		for (Object object : objects)
		{
			comboBox_1.addItem(object);
		}
	}

	public Status getStatusType()
	{
		return (Status) comboBox_2.getSelectedItem();
	}

	public void addStatusActionListener(ActionListener actionListener)
	{
		comboBox_2.addActionListener(actionListener);
	}

	public void addFilteringActionListener(ActionListener actionListener)
	{
		comboBox_1.addActionListener(actionListener);
	}

	public void addTableMouseListener(MouseAdapter mouseListener)
	{
		table.addMouseListener(mouseListener);
	}

	public void addCreateActionListener(ActionListener actionListener)
	{
		btnNew.addActionListener(actionListener);
	}

	public void addUpateActionListener(ActionListener actionListener)
	{
		btnEdit.addActionListener(actionListener);
	}

	public void addDeleteActionListener(ActionListener actionListener)
	{
		btnRemove.addActionListener(actionListener);
	}

	public void addSortingActionListener(ActionListener actionListener)
	{
		comboBox.addActionListener(actionListener);
	}

	public Object getSelectedItem()
	{
		return table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
	}

	public void addSearchActionListener(ActionListener actionListener)
	{
		btnSearch.addActionListener(actionListener);
	}

	public Object[] getSelectedItems()
	{
		ArrayList<Object> items = new ArrayList<Object>();
		for (int i : table.getSelectedRows())
		{
			for (int j : table.getSelectedColumns())
			{
				items.add(table.getValueAt(i, j));
			}
		}
		return items.toArray();
	}

	public void setPaginationCount(int paginationCount)
	{
		pagination.setCount(paginationCount);
	}

	public void setPaginationTotal(int paginationTotal)
	{
		pagination.setTotal(paginationTotal);
	}

	public void refresh()
	{
		pagination.refresh();
	}

	public void addTableRow(Object[] tableRowData)
	{
		tableModel.addRow(tableRowData);
	}

	public void clearTableRow()
	{
		tableModel.setRowCount(0);
	}

	public SortBy getSortType()
	{
		return (SortBy) comboBox.getSelectedItem();
	}

	public Type getItemType()
	{
		try
		{
			return (Type) comboBox_1.getSelectedItem();
		}
		catch (Exception ex)
		{
			return null;
		}
	}

	public void setPagination(AbstractPagination pagination)
	{
		if (this.pagination != null)
		{
			this.remove(this.pagination);
			this.pagination = null;
		}
		this.pagination = pagination;
		this.add(pagination, BorderLayout.SOUTH);
	}

	public void addResetActionListener(ActionListener actionListener)
	{
		btnReset.addActionListener(actionListener);
	}

	public void addAdminToolBar()
	{
		toolBarPanel.add(toolBar);
	}
}
